package com.example.nick.bodyapplication.number

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.example.nick.bodyapplication.R
import io.reactivex.disposables.Disposable
import kotlinx.coroutines.*

class NumberActivity : AppCompatActivity(), CoroutineScope by MainScope() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_number)
    }

    suspend fun doOne(): String {
        Log.i("kt", "kt doOne ${Thread.currentThread().name}")
        delay(100)
        Log.i("kt", "kt doOne ok ${Thread.currentThread().name}")
        return "aaa"
    }

    suspend fun doTwo(): Int {
        Log.i("kt", "kt doTwo ${Thread.currentThread().name}")
        delay(1000)
        Log.i("kt", "kt doTwo ok ${Thread.currentThread().name}")
        return 2
    }

    private fun doShow(await: String, await1: Int) {
        Log.i("kt", "kt doShow ${Thread.currentThread().name}, $await, $await1")
    }

    fun testKtLaunch(view: View) {
//        val coroutineScope = CoroutineScope()
//        coroutineScope.launch {
//            getImage(imageId)
//        }
        launch(Dispatchers.Main) {
            val a = async(Dispatchers.IO) { doOne() }
            val b = async(Dispatchers.Default) { doTwo() }
            doShow(a.await(), b.await())
        }
//        launch {
//            Log.i("kt", "kt launch ${Thread.currentThread().name}")
//            withContext(Dispatchers.IO)  {
//                Log.i("kt", "kt withContext 0 ${Thread.currentThread().name}")
//                delay(3000)
//                Log.i("kt", "kt withContext 0 ${Thread.currentThread().name}")
//            }
//            withContext(Dispatchers.Main){
//                Log.i("kt", "kt withContext 1 ${Thread.currentThread().name}")
//            }
//            Log.i("kt", "kt end ${Thread.currentThread().name}")
//        }
//        GlobalScope.launch {
//
//        }
//        runBlocking{
//            Log.i("kt", "kt runBlocking ${Thread.currentThread().name}")
//            launch(Dispatchers.IO)  {
//                delay(500)
//                Log.i("kt", "kt launch ${Thread.currentThread().name}")
//                withContext(Dispatchers.Main){
//                    Log.i("kt", "kt withContext ${Thread.currentThread().name}")
//                }
//            }
//            Log.i("kt", "kt end ${Thread.currentThread().name}")
//        }
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }

    fun testKtCancel(view: View) {
        cancel()
    }
}